package com.wm.blog_config.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/***
 * 线程池配置
 *@Author: wm_yu
 * @Create_time: 15:01 2020-3-13
 */
@Slf4j
@EnableAsync
@Configuration
public class ThreadConfig implements AsyncConfigurer {

	static {
		//配置异常错误捕获
		Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
	}

	@Bean
	public ThreadPoolTaskExecutor taskExecutor() {

		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setThreadNamePrefix("BLOG-Thread-");
		executor.setCorePoolSize(15);
		executor.setKeepAliveSeconds(300);
		executor.setWaitForTasksToCompleteOnShutdown(true);
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
		executor.setMaxPoolSize(100);
		return executor;
	}

	static class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler{

		@Override
		public void uncaughtException(Thread t, Throwable e) {
			log.error("========{}",e);
		}
	}

}
